<?php
class Theloai extends CI_Controller
{
      public function __construct(){
        parent::__construct();
		$this->load->library('encrypt');
		$this->load->library('session');
		$this->load->helper(array('form', 'url'));
        $this->load->database();
		$this->load->library('pagination');
        $this->load->model('game');
		$this->load->library('form_validation');
			
		}
		//hàm kiểm tra xem đã đăng nhập chưa
	    private function check_isvalidated(){
        if(! $this->session->userdata('validated'))
		{
            redirect('theloai/login');
		return false;
		}
		else
		{
			return true;
			}
        }
		//hàm show thể loại game
		function theloai_game()
		{
			$data['data']=$this->game->list_theloai();
			$data['show']='theloai_game';
			$this->load->view('layout',$data);
		}
		//hàm thêm mới	
		function themmoi()
		{
			if($this->check_isvalidated()==TRUE)
			{
			$data['user']=$this->session->userdata('validated');	
			$data['danhmuc']=$this->game->theloai();
		    $data['error']= '';
			$data['show']='themmoi';
			$this->load->view('layouts_admin',$data);
			}
		}
		//Hàm thêm dữ liệu
			function insert()
		{
		$config['upload_path'] = 'public/uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
     
		$this->load->library('upload',$config);
		$data['madanhmuc']=$_POST['danhmuc'];
		$data['tieude']=$_POST['txt_tittle'];
		$data['link']=$_POST['txt_link'];
		$data['noidung']=$_POST['content'];
		$data['ngay']=date("Y-m-d");
		
		if(!$this->upload->do_upload())
		{
		$this->load->model('game');
		
		$data['danhmuc']=$this->game->list_theloai();
			$data['error']=$this->upload->display_errors();
			$data['show']='themmoi';
			
			$this->load->view('layouts_admin',$data);
	
			}
		else
		{
			$data['upload_data']=$this->upload->data();
			$data['anh']=$data['upload_data']['file_name'];
			
			$this->game->insert($data);
		
			redirect('theloai/manager');
			}	
			}
		//Hàm manager	
	     function manager()
		{
			if($this->check_isvalidated()==TRUE)
			{
	$data['user']=$this->session->userdata('validated');			
	$config['base_url']=base_url().'index.php/theloai/manager';
	$config['full_tag_open']='<p>';
	$config['full_tag_close']='</p>';
	$config['first_link']='Đầu Tiên';
	$config['last_link']='Cuối Cùng';
	$config['total_rows']=$this->game->countAll();
	$data['all']=$this->game->countAll();
	$start=$this->uri->segment(3,0);
	$config['per_page']=8;
    $this->pagination->initialize($config);
	$data['data']=$this->game->getAll($config['per_page'],$start);
	$data['show']='quanly_tin';
	$this->load->view('layouts_admin',$data);
			}
			}
		//Hàm lọc thể loại	
		function filter($id)
		{
	$config['base_url']=base_url()."index.php/theloai/filter/$id";
	$config['per_page']=15;
	$config['uri_segment'] =4;
	$config['full_tag_open']='<p>';
	$config['full_tag_close']='</p>';
	$config['first_link']='Đầu Tiên';
	$config['last_link']='Cuối Cùng';
	$config['total_rows']=$this->game->countAll_id($id);
	$start=$this->uri->segment(4,0);
	$this->pagination->initialize($config);
	$data['data']=$this->game->filter($config['per_page'],$start,$id);
	$data['show']='danhmuc_game';
	$this->load->view('layout',$data);
			}
		function detail($id)
		{
			$data['data']=$this->game->detail($id);
			$data['show']='chitiet';
			$this->load->view('layout',$data);
		}
		//Hàm getTin
		function getTin($id)
		{
			if($this->check_isvalidated()==TRUE)
			{
		$data['user']=$this->session->userdata('validated');	
		$data['danhmuc']=$this->game->theloai();
		$data['data']=$this->game->detail($id);
		$data['error']= '';
			$data['show']='update';
		$this->load->view('layouts_admin',$data);
			
			}
			}
		//HÀM UPDATE	
		function update($id)
		{
		$config['upload_path'] = 'public/uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
		$this->load->library('upload',$config);
		$data['magame']=$id;
		$data['game']=$_POST['txt_tieude'];
		$data['madanhmuc']=$_POST['sel_dm'];
	    $data['noidung']=$_POST['text'];
		//$data['tieude']=$_POST['txt_tittle'];
		$data['link']=$_POST['txt_link'];
		$data['trangthai']=$_POST['sel_tt'];
		$this->upload->do_upload();
	    $data['upload_data']=$this->upload->data();
		$data['anh']=$data['upload_data']['file_name'];
		$data['size']=$data['upload_data']['file_size'];
		$data['hide']=$_POST['sel_hide'];
		$data['status']=$_POST['sel_status'];
	
		$this->game->update($data);
		redirect('/theloai/manager/', 'refresh');
			}
		//Hàm show các game hay nhất
		function haynhat()
		{
	$config['base_url']=base_url().'index.php/theloai/haynhat';
	$config['per_page']=15;
    $config['full_tag_open']='<p>';
	$config['full_tag_close']='</p>';
	$config['first_link']='Đầu Tiên';
	$config['last_link']='Cuối Cùng';
	$config['total_rows']=$this->game->countAll_haynhat();
	$start=$this->uri->segment(3,0);
	$this->pagination->initialize($config);
	$data['data']=$this->game->haynhat($config['per_page'],$start);
	$data['show']='danhmuc_game';
	$this->load->view('layout',$data);
			}
		function moinhat()
		{
			
			$config['base_url']=base_url().'index.php/theloai/moinhat';
	$config['per_page']=15;

	$config['full_tag_open']='<p>';
	$config['full_tag_close']='</p>';
	$config['first_link']='Đầu Tiên';
	$config['last_link']='Cuối Cùng';
	$config['total_rows']=$this->game->countAll_moinhat();
	$start=$this->uri->segment(3,0);
	$this->pagination->initialize($config);
	$data['data']=$this->game->moinhat($config['per_page'],$start);
	$data['show']='game_moinhat';
	$this->load->view('layout',$data);
			}		
									
		function hide($id)
		{
			$this->game->hide($id);
			redirect('/theloai/manager/', 'refresh');
			}
		function un_hide($id)
		{
			
			$this->game->un_hide($id);
			redirect('/theloai/manager/', 'refresh');
			}	
		function link($id)
		{
			$this->game->link($id);
			$link=$this->game->detail($id);
			$lienket=$link->link;
			redirect($lienket);
	
			}
			
		function maxdown()
		{
	$config['base_url']=base_url().'index.php/theloai/maxdown';
	$config['full_tag_open']='<p>';
	$config['full_tag_close']='</p>';
	$config['first_link']='Đầu Tiên';
	$config['last_link']='Cuối Cùng';
	$config['total_rows']=$this->game->countAll();
	$start=$this->uri->segment(3,0);
	$config['per_page']=15;
    $this->pagination->initialize($config);
	$data['data']=$this->game->maxdown($config['per_page'],$start);
	$data['show']='danhmuc_game';
	$this->load->view('layout',$data);
			}
		//Hàm Login
		//LOGIN
		//Các hàm về login
		function login()
		{
		$this->form_validation->set_rules('username', 'Username', 'required|callback_checklogin');
        $this->form_validation->set_rules('password', 'Password', 'required');
		if ($this->form_validation->run() == FALSE)
		{
		$data['user']=$this->session->userdata('validated');
		$data['show']="login";
		$this->load->view('layouts_admin',$data);
		}
		else
		{
		$data['password']=$this->input->post('password');
		$data['username']=$this->input->post('username');
		//$data['stt_domain']=$this->getDomain();
		$this->game->session($data);
		redirect('theloai/manager','refresh');
		}
		}
		//Hàm check login
		function checklogin($username)
		{
		$data['username']=$username;
		$data['pass']=$this->input->post('password');
		//$data['stt_domain']=$this->getDomain();
		$q=$this->game->check_login($data);
		if($q==1)
		{
		return true;
		}
		else
		{
		$this->form_validation->set_message('checklogin','Bạn đã nhập sai user và pass');
		return false;
		}
		}
		//Hàm logout
		function logout()
		{$this->session->sess_destroy();
		redirect('theloai/login');
			}
		//Hàm đổi pass
	function changepass()
   {
	   if($this->check_isvalidated()==TRUE)
	   {
	   $data['user']=$this->session->userdata('validated');
	   $this->form_validation->set_rules('oldpass','Old Pass','trim|required|callback_passcheck');
	   $this->form_validation->set_rules('newpass','New Pass','trim|required|min_length[5]|max_length[10]');
	   $this->form_validation->set_rules('cfpass','Confirm Pass','trim|required|matches[newpass]|min_length[5]|max_length[10]');
	   if($this->form_validation->run()==FALSE)
	   {
		   $data['show']='changepass';
		    $this->load->view('layouts_admin',$data);
		   }
		  else
		  {
			  $data['acc']=$this->session->userdata('user');
			  $data['newpass']=$_POST['newpass'];
			  $this->game->changepass($data);
			  $data['show']='success';
			  $this->load->view('layouts_admin',$data);
			  } 
	   
	
	   }}
	//Kiểm tra pass
	function passcheck($str)
	{
		$data['pass']=$str;
		$data['user']=$this->session->userdata('user');
		$q=$this->game->passcheck($data);
		if($q==1)
		{
			return true;
			}
		else
		{
			$this->form_validation->set_message('passcheck','Password không chính xác');
			return false;
			}	
		
		}
						
		
}
